package com.example.haoqisystem.controller;

import com.example.haoqisystem.Utils.WordUtils.ExportWord;
import com.example.haoqisystem.Utils.excelUtils.ExportExcel;
import com.example.haoqisystem.entity.RegistrationForm;
import com.example.haoqisystem.request.CreateAndUpdateRegistrationFormRequest;
import com.example.haoqisystem.request.QueryRegistrationFormListRequest;
import com.example.haoqisystem.response.BaseResponse;
import com.example.haoqisystem.response.PageResult;
import com.example.haoqisystem.service.RegistrationFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor(onConstructor_ ={@Autowired} )
@Api(tags = "报名表api")
@RequestMapping("/registrationForm")
public class RegistrationFormController {

    private final RegistrationFormService registrationFormService;

    @ApiOperation("下载报名表（报名表模板)")
    @GetMapping("/downWordForm")
    public void generateDocument(HttpServletResponse response) {
        //TODO：这里报名表的排版很大的问题
        ExportWord<RegistrationForm> exportWord = new ExportWord<>(); // 创建 ExportWord 实例，类型参数可以是任意对象，因为不会使用到具体的数据
        String title = "浩奇员工培训报名表"; // 设置文档标题
        RegistrationForm form=new RegistrationForm();
        form.setCompanyName("hhhh");
        form.setGender("女");
        form.setEmail("2155@wde55");
        form.setName("eded");
        form.setTechnicalLevel("25555");

        List<RegistrationForm> dataset = new ArrayList<>(); // 使用空集合作为数据集合，不会填充具体数据
        dataset.add(form);
        exportWord.exportWord(title, dataset, response);
    }

    @ApiOperation("导出excel名单")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        try {
            // 获取报名表数据集合（假设你已经从数据库或其他数据源中获取到了数据）
            List<RegistrationForm> registrationForms = registrationFormService.getList();
            // 创建 ExportExcel 对象
            ExportExcel<RegistrationForm> exportExcel = new ExportExcel<>();
            // 设置导出的标题
            String title = "报名表名单";
            // 调用工具类的方法进行导出
            exportExcel.downloadExcelFile(title, registrationForms, response);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常情况，例如返回错误信息等
        }
    }

    @ApiOperation("提交报名表")
    @PostMapping("/add")
    public BaseResponse<RegistrationForm> add(@RequestBody CreateAndUpdateRegistrationFormRequest request){
        return registrationFormService.add(request);
    }

    @ApiOperation("更新报名表")
    @PutMapping("/update")
    public BaseResponse<RegistrationForm> update(@RequestBody CreateAndUpdateRegistrationFormRequest request){
        return registrationFormService.update(request);
    }

    @ApiOperation("删除报名表")
    @GetMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "报名表id",required = true)
    })
    public BaseResponse<String> delete(Long id){
        return registrationFormService.delete(id);
    }

    @ApiOperation("分页查询报名表")
    @GetMapping("/queryPageList")
    //TODO:是否需要查询条件？
    public BaseResponse<PageResult<RegistrationForm>> queryList(QueryRegistrationFormListRequest request){
        return registrationFormService.queryList(request);
    }

    @ApiOperation("报名表列表查询，不分页")
    @GetMapping("/queryList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "学员姓名"),
            @ApiImplicitParam(name = "lectureName",value = "课程名"),
            @ApiImplicitParam(name = "companyName",value = "公司名"),
            @ApiImplicitParam(name = "lectureId",value = "课程id",dataType = "Long"),
            @ApiImplicitParam(name = "id",value = "id",dataType = "Long")
    })
    public BaseResponse<List<RegistrationForm>> queryList1(String name
            ,String lectureName,String companyName,Long lectureId,Long id){
        return registrationFormService.queryList1(name,lectureName,companyName,lectureId,id);
    }

    @ApiOperation("跟据id查询报名表详情")
    @GetMapping("/detail")
    public BaseResponse<RegistrationForm> detail(@RequestParam Long id,@RequestParam boolean deleted){
        return registrationFormService.detail(id,deleted);
    }
}
